package com.hsp.smallChange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {
    // 1. 先完成显示菜单，并可以选择菜单，给出对应提示


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String key = "";
        boolean loop = true;
        //2. 完成零钱通明细：（1）可以把收益入账和消费，保存到数组（2）可以使用对象（3）可以使用String拼接
        String details = "----------------零钱通明细-----------------";
        //3. 收益入账
        double money = 0;
        double balance = 0;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以用于日期格式的转换
        //4. 消费
        //定义新变量，保存消费的原因
        String note = null;




        do {
            System.out.println("==========零钱通菜单==========");
            System.out.println("\t\t1. 零钱通明细");
            System.out.println("\t\t2. 收益入账");
            System.out.println("\t\t3. 消费");
            System.out.println("\t\t4. 退出");

            System.out.print("请选择1-4");
            key = scanner.next();
            //使用switch分支结构
            switch (key) {
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.println("收益入账金额");
                    money = scanner.nextDouble();
                    if(money<=0){
                        System.out.println("收益入账金额需要大于零");
                        break;
                    }
                    //money的值校验

                        date = new Date();
                        balance += money;
                        //拼接收益入账信息到details
                        details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
                        System.out.println(details);
                        break;

                case "3":
                    System.out.print("消费金额");
                    money = scanner.nextDouble();
                    //范围校验,找出金额不正确的情况
                    if(money<=0||money>balance){
                        System.out.println("消费金额应该在0-"+balance);
                        break;
                    }

                    System.out.println("消费说明：");
                    note = scanner.next();
                    balance -= money;
                    //拼接消费信息
                    date = new Date();
                    details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;

                case "4":
                    String choice = "";
                    //判断用户是否要退出
                    while (true) {
                        System.out.print("你确定要退出吗？ y/n\t");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                    }
                    if(choice.equals("y")){
                        loop=false;
                        break;
                    }
                    //当用户退出循环以后，在判断
                    break;
                default:
                    System.out.println("菜单选择有误，请重新选择");
            }
        } while (loop);
        System.out.println("退出了零钱通项目");
    }
}
